SwapIt / app / api / auth / [...nextauth] / route.js
route.js
Raw
import nextAuth from "next-auth";
import GoogleProvider from 'next-auth/providers/google';

// import {op} from "../../../../utils/user.mjs";
// import {schema} from "../../../../utils/database.mjs";
import {op} from "../../../../utils/user.mjs"
const handler = nextAuth({
    providers: [
        GoogleProvider({
            clientId: process.env.GOOGLE_CLIENT_ID,
            clientSecret: process.env.GOOGLE_CLIENT_SECRET,
            authorization: {
                params: {
                  prompt: "consent",
                  access_type: "offline",
                  response_type: "code"
                }
              },
              authorization: {
                params: {
                  scope: "openid https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"
                },
              },
            // scope: "https://www.googleapis.com/auth/drive"
            // client: {
            //     tokenHost: "https://oauth2.googleapis.com",
            //     tokenPath: "/token",
            //     revokePath: "/revoke",
            // },
        })
    ],
    callbacks: {
        jwt: ({token, account })=> {
              if (account?.access_token) {
                token.access_token = account.access_token;
              }
              return token;
        }, 
        async session({session, token}){
            const sessionUser = await op.findEmail(session.user.email);
            session.user.id =  sessionUser.data[0];
            session.token = token
            return session;
        },
        async signIn({profile}){
            const userExists = await op.findEmail(profile.email);
            if(!userExists.found){
                await op.CreateUser(profile.name,profile.email,"null","null","null",(profile.picture));
            }
            return true;
        },
    }

})

export { handler as GET, handler as POST}